package io.netty.myExample.zhanglongp8类聊天室;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.myExample.zhanglong.p8类聊天室.ChatClientHandler;
import io.netty.util.CharsetUtil;

/**
 * netty初始化器，channel 注册之后，会执行里面相应的初始化方法
 */
public class ChatClientInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //添加分割符处理器
       // pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
        //添加加密器
        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
        //添加服务器自定义处理器
        pipeline.addLast(new ChatClientHandler());

    }
}
